<template>
	<div class="system-dic-container layout-padding">
		<el-card shadow="hover" class="layout-padding-auto">
			<div class="system-user-search mb15">
				<el-button size="default" type="success" v-auth="'/system/affiche/add'"  @click="onOpenAdd('add')">
					<el-icon>
						<ele-FolderAdd />
					</el-icon>
					新增公告
				</el-button>
			</div>
			<el-table :data="state.tableData.data" v-loading="state.tableData.loading" style="width: 100%">
				<el-table-column prop="id" label="ID" show-overflow-tooltip width="80px"></el-table-column>
				<el-table-column prop="title" label="公告标题" show-overflow-tooltip></el-table-column>
				<el-table-column prop="remark" label="公告描述" show-overflow-tooltip></el-table-column>
				<el-table-column label="创建时间" show-overflow-tooltip width="220px">
					<template #default="scope">
						<span>{{ dateFormatYMDHMS(scope.row.create_time * 1000)}}</span>
                    </template>
				</el-table-column>
				<el-table-column label="操作" width="150">
					<template #default="scope">
						<!-- <el-button size="small" text type="primary" v-auth="'/system/affiche/edit'"  @click="onOpenEdit('edit', scope.row)">修改</el-button> -->
						<el-button size="small" text type="primary" v-auth="'/system/affiche/del'"  @click="onRowDel(scope.row)">删除</el-button>
					</template>
				</el-table-column>
			</el-table>
			<el-pagination
				@size-change="onHandleSizeChange"
				@current-change="onHandleCurrentChange"
				class="mt15"
				:pager-count="5"
				:page-sizes="[10, 20, 30]"
				v-model:current-page="state.tableData.param.page"
				background
				v-model:page-size="state.tableData.param.limit"
				layout="total, sizes, prev, pager, next, jumper"
				:total="state.tableData.total"
			>
			</el-pagination>
		</el-card>
		<addEditDialog ref="addEditDialogRef" @refresh="getTableData()" />
	</div>
</template>

<script setup name="affiche">
import { defineAsyncComponent, reactive, onMounted, ref } from 'vue';
import { ElMessageBox, ElMessage } from 'element-plus';
import { afficheApi } from '/@/api/affiche/index';
import commonFunction from '/@/utils/commonFunction';

// 定义变量内容
const { dateFormatYMDHMS } = commonFunction();
// 引入组件
const addEditDialog = defineAsyncComponent(() => import('./dialog.vue'));

// 定义变量内容
const addEditDialogRef = ref();
const state = reactive({
	tableData: {
		data: [],
		total: 0,
		loading: false,
		param: {
			page: 1,
			limit: 10,
		},
	},
});

// 初始化表格数据
const getTableData = () => {
	state.tableData.loading = true;
    afficheApi().index(state.tableData.param).then(res=>{
        state.tableData.data = res.data.list;
        state.tableData.total = res.data.total;
        state.tableData.loading = false;
    })
};
// 打开新增平台弹窗
const onOpenAdd = (type) => {
	addEditDialogRef.value.openDialog(type);
};
// 打开修改平台弹窗
const onOpenEdit = (type, row) => {
	addEditDialogRef.value.openDialog(type, row);
};
// 删除平台
const onRowDel = (row) => {
	ElMessageBox.confirm(`此操作将永久删除公告标题：“${row.title}”，是否继续?`, '提示', {
		confirmButtonText: '确认',
		cancelButtonText: '取消',
		type: 'warning',
	}).then(() => {
			afficheApi().del({ids:row.id}).then(res=>{
                getTableData();
                ElMessage.success('删除成功');
            })
		})
		.catch(() => {});
};
// 分页改变
const onHandleSizeChange = (val) => {
	state.tableData.param.limit = val;
	getTableData();
};
// 分页改变
const onHandleCurrentChange = (val) => {
	state.tableData.param.page = val;
	getTableData();
};
// 页面加载时
onMounted(() => {
	getTableData();
});
</script>
